package com.sys.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.domain.Dict;
import com.sys.domain.DictItem;
import com.sys.result.PageParam;
import com.sys.result.RequestPageVo;
import com.sys.result.RequestResultVo;
import com.sys.service.DictItemService;
import com.sys.service.DictService;

@Controller
@RequestMapping("/admin/dict")
public class DictController {
	
	@Resource
	private DictService dictService;
	
	@Resource
	private DictItemService dictItemService;
	
	//到字典项列表页面
	@RequestMapping("dictPage")
	public String toDictPage(Model model, @RequestParam("title") String title){
		model.addAttribute("title", title);
		return "dict/dictPage";
	}
	
	//查询字典项列表
	@RequestMapping("queryDictList")
	public @ResponseBody RequestPageVo<Dict> queryDictList(PageParam param,String dictName,String dictCode){
		return dictService.queryDictList(param,dictName,dictCode);
	}
	
	//删除字典项
	@RequestMapping("deleteDict")
	public @ResponseBody RequestResultVo<Dict> deleteDict(Integer id){
		return dictService.deleteDict(id);
	}
	
	//添加或修改字典项
	@RequestMapping("saveOrUpdateDict")
	public @ResponseBody RequestResultVo<Dict> saveOrUpdateDict(Dict dict){
		RequestResultVo<Dict> result;
		if(dict.getId()!=null){//
			result=dictService.updateDict(dict);
		}else{
			result=dictService.addDict(dict);
		}
		return result;
	}
	
	//添加或修改字典值
	@RequestMapping("saveOrUpdateDictItem")
	public @ResponseBody RequestResultVo<DictItem> saveOrUpdateDictItem(DictItem dictItem){
		RequestResultVo<DictItem> result;
		if(dictItem.getId()!=null){//
			result=dictItemService.updateDictItem(dictItem);
		}else{
			result=dictItemService.addDictItem(dictItem);
		}
		return result;
	}
	
	//到字典值列表页面
	@RequestMapping("toDictItemPage")
	public String toDictItemPage(Model model,String dictCode){
		model.addAttribute("dictCode", dictCode);
		return "dict/dictItemPage";
	}
	
	//查询字典值列表
	@RequestMapping("queryDictItemList")
	public @ResponseBody RequestPageVo<DictItem> queryDictItemList(PageParam param,String itemName,String dictCode){
		return dictItemService.queryDictItemList(param,itemName,dictCode);
	}
	
	//删除字典值
	@RequestMapping("deleteDictItem")
	public @ResponseBody RequestResultVo<DictItem> deleteDictItem(Integer id){
		return dictItemService.deleteDictItem(id);
	}
	
}
